﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Exceptions;


namespace Wekit.Repository
{
    public abstract class BaseRepository<T>
    {
        public ISession _session;
        public ISessionFactory SessionFactory { get; set; }

       /// <summary>
       /// 获取当前的会话程序
       /// </summary>
        public ISession CurrentSession { get {
            if (_session != null)
                return _session;
            return SessionFactory.GetCurrentSession();
        } }


        /// <summary>
        /// 开启一个新的会话
        /// </summary>
        public ISession OpenSesson {
            get {
                return SessionFactory.OpenSession();
            }
        }

        /// <summary>
        /// 获取当前线程的Sesson对象
        /// </summary>
        /// <returns></returns>
        public ISession getSession()
        {
            return getSession(true);
        }

        /// <summary>
        /// 根据不通的条件来获取不同的Session对象
        /// </summary>
        /// <param name="isCurrent">如果为true 返回当前的会话，当为false 是打开一个新的会话</param>
        /// <returns>ISession 对象</returns>
        public ISession getSession(bool isCurrent)
        {
            if (isCurrent)
                return CurrentSession;
            else
                return OpenSesson;
        }

        /// <summary>
        /// 通用的函数添加功能
        /// </summary>
        /// <param name="obj">需要添加的对象</param>
        /// <param name="isCurrentSession">是否获取当前线程</param>
        /// <returns></returns>
        protected  T add(T obj,bool isCurrentSession)
        {
            ISession session = getSession(isCurrentSession);
            ITransaction transaction = session.BeginTransaction();
            try
            {
                session.Save(obj);
                session.Flush();
            }
            catch (GenericADOException ex)
            {
                transaction.Rollback();
                throw ex;
            }
            transaction.Commit();
            return obj;
        }


        /// <summary>
        /// 根据实体的ID来获取实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isCurrentSession"></param>
        /// <returns></returns>
        protected T get(object id, bool isCurrentSession)
        {
            ISession session = getSession(isCurrentSession);
            return session.Get<T>(id);
        }


        /// <summary>
        /// 根据实体的ID加载实体
        /// </summary>
        /// <param name="id">实体标识</param>
        /// <param name="isCurrentSession"></param>
        /// <returns></returns>
        protected T load(object id, bool isCurrentSession)
        {
            ISession session = getSession(isCurrentSession);
            return session.Load<T>(id);
        }

        /// <summary>
        /// 删除单个实体对象
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="isCurrentSession"></param>
        /// <returns></returns>
        protected bool delete(T obj, bool isCurrentSession)
        {
            ISession session = getSession(isCurrentSession);
            ITransaction transaction=session.BeginTransaction();
            try{
                session.Delete(obj);
                session.Flush();
                
            }catch(Exception)
            {
              transaction.Rollback();
              return false;
            }
           transaction.Commit();
           return true;
        }

        /// <summary>
        /// 根据查询条件获取对象
        /// </summary>
        /// <param name="queryString"></param>
        /// <param name="isCurrentSession"></param>
        /// <returns></returns>
        protected IList<T> query(string queryString, bool isCurrentSession)
        {
            ISession session = getSession(isCurrentSession);
            IQuery query = session.CreateQuery(queryString);
            return query.List<T>();
        }

        /// <summary>
        /// 创建查询对象
        /// </summary>
        /// <param name="isCurrentSession"></param>
        /// <returns></returns>
        protected ICriteria createCriteria(bool isCurrentSession)
        {
            ISession session = getSession(isCurrentSession);
            return session.CreateCriteria(typeof(T));
        }

    }
}
